package com.hctg.forced2sleep.easy_window;

import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityNodeInfo;

import com.actor.myandroidframework.utils.ConfigUtils;
import com.hctg.forced2sleep.databinding.EasyWindowPageElementSelectBinding;
import com.hctg.forced2sleep.utils.AccessibilityUtils;
import com.hjq.window.EasyWindow;

import java.util.ArrayList;
import java.util.List;

/**
 * description: 页面元素选择
 * company    :
 *
 * @author : ldf
 * date       : 2024/10/5 on 17
 * @version 1.0
 */
public class PageElementSelectEasyWindow extends EasyWindow2<PageElementSelectEasyWindow> implements EasyWindow.OnClickListener<View> {

    private final EasyWindowPageElementSelectBinding viewBinding;
    private final List<AccessibilityNodeInfo> list  = new ArrayList<>();
    private String pageName;
    private boolean isCancel = false;

    public PageElementSelectEasyWindow() {
        super(ConfigUtils.APPLICATION);
        viewBinding = EasyWindowPageElementSelectBinding.inflate(LayoutInflater.from(getContext()), (ViewGroup) getDecorView(), false);
        setContentView(viewBinding.getRoot());
        setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        setTag(this.getClass().getName());
        //设置全屏, 不要状态栏
//        addWindowFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
////        addWindowFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
//        WindowManager.LayoutParams windowParams = getWindowParams();
//        if (windowParams != null) {
//            systemUiVisibility = windowParams.systemUiVisibility;
//        }
//        setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN);

//        setOnClickListener(viewBinding.btn.getId(), this);

//        post(new Runnable() {
//            @Override
//            public void run() {
//                //显示在最上面
//                easyWindow.show();
//            }
//        });
        final int[] i = {99999};
        postDelayed(new Runnable() {
            @Override
            public void run() {
                if (-- i[0] < 0) return;
                AccessibilityNodeInfo accessibilityNodeInfo = AccessibilityUtils.getRootNodeInfo();
//                com.actor.myandroidframework.utils.LogUtils.errorFormat("accessibilityNodeInfo: %s", accessibilityNodeInfo);
                if (accessibilityNodeInfo == null || accessibilityNodeInfo.getPackageName() == null) {
                    if (!isCancel) {
                        postDelayed(this, 100L);
                    }
                } else {
//                    LogUtils.json(accessibilityNodeInfo);
                    //页面名称?: Activity/Dialog/FrameLayout ... 名称
//                    pageName = AccessibilityUtils.getPageName();

                    //获取当前Activity的名称, 获取不到Dialog名称
//                    ComponentName processName = UsageStatsUtils.getProcessName();
//                    LogUtils.error(processName);
//                    if (processName == null) {
//                        LogUtils.error("processName = null!");
//                    } else {
//                        pageName = processName.getClassName();
//                        LogUtils.errorFormat("processName: PackageName=%s, ClassName=%s", processName.getPackageName(), pageName);
//                    }

                    //??名称
                    CharSequence className = accessibilityNodeInfo.getClassName();
                    if (className != null) {
                        pageName = className.toString();
                    }

                    AccessibilityUtils.findAllNodes(accessibilityNodeInfo, list, true);
                    boolean hasStatusBar = AccessibilityUtils.hasStatusBar();
                    com.actor.myandroidframework.utils.LogUtils.errorFormat("hasStatusBar: %b", hasStatusBar);
//                    LogUtils.json(list);
                    viewBinding.getRoot().setPageInfos(hasStatusBar, list);
                    viewBinding.getRoot().setOnRectClickListener((rectRoot, rect, hasStatusBar1) -> {
                        if (existShowingByClass(RectMenuEasyWindow.class)) {
                            RectMenuEasyWindow easyWindow = getWindowByClass(RectMenuEasyWindow.class);
                            if (easyWindow != null) {
                                easyWindow.updatePosition(rectRoot, rect, hasStatusBar1);
                            }
                        } else {
                            RectMenuEasyWindow easyWindow = new RectMenuEasyWindow(rectRoot, rect, hasStatusBar1);
                            easyWindow.show();
                            easyWindow.setOnItemClickListener((rectRoot1, rect1, position) -> {
                                switch (position) {
                                    case 0: //标记元素
                                        if (rect1 == null) break;
                                        Rect rect3 = new Rect();
                                        for (AccessibilityNodeInfo nodeInfo : list) {
                                            nodeInfo.getBoundsInScreen(rect3);
                                            if (rect3.equals(rect1)) {
                                                //取消一些EasyWindow
                                                EasyWindow.recycleByClass(RectMenuEasyWindow.class);
                                                EasyWindow.recycleByClass(PageElementSelectEasyWindow.class);
                                                new MarkElementEasyWindow(nodeInfo, pageName).show();
                                                break;
                                            }
                                        }
                                        break;
                                    case 1: //忽略元素
                                        if (rect1 == null) break;
                                        //根元素不要移除
                                        if (rect1.equals(rectRoot1)) break;
                                        Rect rect2 = new Rect();
                                        for (int i1 = 0; i1 < list.size(); i1++) {
                                            AccessibilityNodeInfo nodeInfo = list.get(i1);
                                            nodeInfo.getBoundsInScreen(rect2);
                                            if (rect2.equals(rect1)) {
                                                list.remove(i1);
                                                viewBinding.getRoot().setPageInfos(hasStatusBar, list);
                                                break;
                                            }
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            });
                        }
                    });
                }
            }
        }, 100L);
    }

    @Override
    public void show() {
        super.show();
        isCancel = false;
    }

    @Override
    public void cancel() {
        isCancel = true;
//        removeWindowFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        setSystemUiVisibility(systemUiVisibility);
        super.cancel();
    }

    @Override
    public void onClick(EasyWindow<?> easyWindow, View view) {
        switch (view.getId()) {
//            case R.id.btn:
//                cancel();
//                break;
            default:
                break;
        }
    }
}
